package com.micromall.controller;

import com.alibaba.fastjson.JSONObject;
import com.micromall.common.base.BaseController;
import com.micromall.common.sms.SmsMessageSender;
import com.micromall.common.sms.SmsSendResult;
import com.micromall.common.utils.Charsets;
import com.micromall.common.utils.JsonResult;
import com.micromall.common.utils.ValidateUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by DanteFung on 2017/7/30.
 */
@Controller
@RequestMapping("/common")
public class CommonController extends BaseController {

    @RequestMapping("/smsCode1")
    @ResponseBody
    public Object getSmsCode(HttpServletRequest request, HttpServletResponse response){
        response.setCharacterEncoding(Charsets.UTF_8.toString());
//        response.setHeader("Content-Type","text/html;charset=utf-8");
        response.setContentType("application/json; charset=UTF-8");
        String phoneNo = request.getParameter("phoneNo");
        SmsMessageSender smsSender = SmsMessageSender.getSingleton();
        String smsCode  = ValidateUtil.genSmsValidateCode();
        String message = String.format("验证码:%s", smsCode);
        if (ValidateUtil.isEmpty(phoneNo)) {
            return write2Json(null, Boolean.FALSE,"phoneNo"+JsonResult.PARAMETER_EMPTY.getDesc(), null);
        }
        SmsSendResult smsSendResult = null;
        logger.info(message);
        try {
            smsSendResult = smsSender.sendSms(new String[]{phoneNo}, message);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(e.getMessage());
        }

        if (null == smsSendResult || smsSendResult.getSuccessCount() == 0) {
            logger.info("短信发送失败!");
            return write2Json(null, Boolean.FALSE,"短信发送失败!", null);
        }
        logger.info("{}:[num={}]:[success={}]:[faile={}]:[err={}]:[errid={}]"
                ,SmsMessageSender.class.getSimpleName()
                ,smsSendResult.getSuccessCount()
                , smsSendResult.getSuccessMobiles()
                , smsSendResult.getFaileMobiles()
                , smsSendResult.getReturnMsg()
                , smsSendResult.getErrorId());

        request.getSession().setAttribute("smsCode", smsCode);
        return write2Json(null, Boolean.TRUE,"短信发送成功!", null);
    }

    @RequestMapping("/smsCode")
    public void getSmsCode2(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding(Charsets.UTF_8.toString());
        response.setContentType("application/json; charset=UTF-8");
        PrintWriter writer = response.getWriter();
        String phoneNo = request.getParameter("phoneNo");
        SmsMessageSender smsSender = SmsMessageSender.getSingleton();
        String smsCode  = ValidateUtil.genSmsValidateCode();
        String message = String.format("验证码:%s", smsCode);
        if (ValidateUtil.isEmpty(phoneNo)) {
            writer.write(JSONObject.toJSONString(write2Json(null, Boolean.FALSE,"phoneNo"+ JsonResult.PARAMETER_EMPTY.getDesc(), null)));
        }
        SmsSendResult smsSendResult = null;
        logger.info(message);
        try {
            smsSendResult = smsSender.sendSms(new String[]{phoneNo}, message);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(e.getMessage());
        }

        if (null == smsSendResult || smsSendResult.getSuccessCount() == 0) {
            logger.info("短信发送失败!");
            writer.write(JSONObject.toJSONString(write2Json(null, Boolean.FALSE,"短信发送失败!", null)));
        }
        logger.info("{}:[num={}]:[success={}]:[faile={}]:[err={}]:[errid={}]"
                ,SmsMessageSender.class.getSimpleName()
                ,smsSendResult.getSuccessCount()
                , smsSendResult.getSuccessMobiles()
                , smsSendResult.getFaileMobiles()
                , smsSendResult.getReturnMsg()
                , smsSendResult.getErrorId());

        request.getSession().setAttribute("smsCode", smsCode);
        writer.write(JSONObject.toJSONString(write2Json(null, Boolean.TRUE,"短信发送成功!", null)));
    }
}
